package com.example.demo.controller;

import com.example.demo.entity.User1;
import com.example.demo.service.User1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// 添加跨域访问支持
@CrossOrigin(origins = "*")  // 允许所有域名和端口
@RequestMapping("/user1")
public class User1Controller {
    @Autowired
    private User1Service user1Service;

    // 修改点：新增 @RequestParam Integer age 参数
    @GetMapping("/getUsers")
    public List<User1> getAllUsers(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer minAge,  // 新增参数：最小年龄
            @RequestParam(required = false) Integer maxAge   // 新增参数：最大年龄
    ) {
        return user1Service.findUsers(name, minAge, maxAge);
    }

    @GetMapping("/list")
    public List<User1> findAll() {
        return user1Service.findAll();
    }

    @GetMapping("/search")
    public List<User1> findByName(@RequestParam String name) {
        return user1Service.findByName(name);
    }

    @PostMapping("/add")
    public String addUser(@RequestBody User1 user) {
        user1Service.addUser(user);
        return "添加成功";
    }

    @PutMapping("/update")
    public String updateUser(@RequestBody User1 user) {
        user1Service.updateUser(user);
        return "更新成功";
    }

    @DeleteMapping("/delete")
    public String deleteUser(@RequestParam String name) {
        user1Service.deleteUser(name);
        return "删除成功";
    }
}